//
//  CkgdVC.m
//  AStage_shougongke
//
//  Created by 赵磊 on 16/1/20.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "CkgdVC.h"
#import "aStageHeader.pch"
@interface CkgdVC ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, retain) UICollectionView *kechengCollectionView;
@property (nonatomic, retain) NSMutableArray *dataArr;
@property (nonatomic, assign) CGFloat number;
@end
@implementation CkgdVC
- (void)dealloc
{
    [_kechengCollectionView release];
    [_dataArr release];
    [_url release];
    [super dealloc];
}
// 页面即将出现 将tabBar显示
- (void)viewWillAppear:(BOOL)animated
{
    // tabBar显示
    self.tabBarController.tabBar.hidden = YES;
}
// 页面即将消失 将tabBar隐藏
- (void)viewWillDisappear:(BOOL)animated
{
    // tabBar隐藏
    self.tabBarController.tabBar.hidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [super viewDidLoad];
    self.number = 40;
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"教程";
    // 隐藏push自动生成的backButton
    [self.navigationItem setHidesBackButton:YES];
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((WIDTH - 30) / 2, (WIDTH - 30) / 2 / 0.618);
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    self.kechengCollectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    _kechengCollectionView.backgroundColor = [UIColor whiteColor];
    _kechengCollectionView.delegate = self;
    _kechengCollectionView.dataSource = self;
    [_kechengCollectionView registerClass:[JiaochengCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [self.view addSubview:_kechengCollectionView];
    // 下拉刷新
    self.kechengCollectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(dataHandler)];
    [self.kechengCollectionView.mj_header beginRefreshing];
    // 上拉加载
    self.kechengCollectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadnewdata)];
    if (_dataArr.count != 0) {
        [self.kechengCollectionView.mj_footer beginRefreshing];
    }
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, HEIGHT - 50, WIDTH, 50)];
    view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view];
    UIButton * fanhui = [UIButton buttonWithType:UIButtonTypeCustom];
    [fanhui setImage:[UIImage imageNamed:@"fanhui@2x"] forState:UIControlStateNormal];
    fanhui.frame = CGRectMake(0, 0, 50, 50);
    [fanhui addTarget:self action:@selector(fanhui) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:fanhui];
    [_kechengCollectionView release];
}
- (void)loadnewdata
{
    [AFNModel GETWithURL:[NSString stringWithFormat:@"http://m.shougongke.com/index.php?&c=Course&a=newCourseList&gcate=allcate&last_id=%lf&order=new&vid=15", self.number] completion:^(id result) {
        NSArray *arrTemp = [NSArray array];
        arrTemp = [RenmenModel modelHandlerWithArray:result[@"data"]];
        for (NSInteger i = 0; i < 20; i++) {
            [self.dataArr addObject:arrTemp[i]];
        }
        [_kechengCollectionView reloadData];
    }];
    [self.kechengCollectionView.mj_footer endRefreshing];
    self.number += 20;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataArr.count;
}
// 返回具体Item
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    JiaochengCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    RenmenModel *m = _dataArr[indexPath.item];
    cell.bgView.backgroundColor = [UIColor changeColorWithString:m.bg_color];
    [cell.photo sd_setImageWithURL:[NSURL URLWithString:m.host_pic]];
    cell.title.text = m.subject;
    cell.writer.text = [NSString stringWithFormat:@"by %@", m.user_name];
    cell.popularity.text = [NSString stringWithFormat:@"%@人气/%@收藏", m.view, m.collect];
    return cell;
}
- (void)dataHandler
{
    [AFNModel GETWithURL:self.url completion:^(id result) {
        NSArray *arrTemp = [NSArray array];
        arrTemp = result[@"data"];
        self.dataArr = [RenmenModel modelHandlerWithArray:arrTemp];
        [self.kechengCollectionView.mj_header endRefreshing];
        [_kechengCollectionView reloadData];
    }];
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    JiaochengxiangqingVC *jcxqVC = [[JiaochengxiangqingVC alloc] init];
    RenmenModel *m = _dataArr[indexPath.item];
    jcxqVC.hand_id = m.hand_id;
    [self.navigationController pushViewController:jcxqVC animated:YES];
}
- (void)fanhui
{
    self.hidesBottomBarWhenPushed = NO;
    self.navigationController.navigationBarHidden = NO;
    [self.navigationController popViewControllerAnimated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
